<?php
namespace Swiftx\Component\Bundle;
use Swiftx\Interfaces\Application\Config as AppConfig;
use Swiftx\Interfaces\Bundle\Config as ConfigInterface;
use Swiftx\Interfaces\Bundle\Kernel;

/**
 * 模块配置基础类
 * @package Swiftx\Component\Bundle
 */
class Config implements ConfigInterface {

    /**
     * 应用程序配置
     * @var AppConfig
     */
    protected $application;

    /**
     * 当前模块
     * @var Kernel
     */
    protected $bundle;

    /**
     * 配置项
     * @var string[]
     */
    protected $data = [];

    /**
     * 初始化配置
     * @param AppConfig $config
     * @param Kernel $bundle
     */
    public function __construct(AppConfig $config, Kernel $bundle){
        $this->application = $config;
        $this->bundle = $bundle;
    }

    /**
     * 获取模块配置
     * @param string $name
     * @param string $default
     * @return string
     */
    public function get(string $name, string $default=null){
        if(isset($this->data[$name])) return $this->data[$name];
        return $this->application->getBundle($this->bundle->getName(),$name, $default);
    }

    /**
     * 获取项目目录
     * @param string $path
     * @return string
     */
    public function dir(string $path):string {
        if($path[strlen($path)-1] != '/') $path = '/'.$path;
        return realpath($this->bundle->getRootPath().$path);
    }

}